package com.example.Server;

import com.example.Mapper.JudgeMapper;
import com.example.Model.JudegeFeginData;
import com.example.Model.JudegeResult;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class JudgeServer {
    @Autowired
    private JudgeMapper Mapper;
    @Autowired
    private iJudgedao _dao;
    public JudegeResult Judge(String UserCode,int qid,String lugType){
        Map<String,Object> result=Mapper.GetQResult(qid);
        String NewCode=MergeCode(UserCode,(String) result.get(lugType));
      //  System.out.println(NewCode);
        Map<String,String> temp=new HashMap<>();
        temp.put("Code",NewCode);
       // System.out.println(result.get("result"));
        JudegeFeginData obj=new JudegeFeginData();
        obj.Code=NewCode;
        obj.input="";
        obj.type=lugType;
        obj.result=(String) result.get("result");
        JudegeResult JudgeResult= _dao.Judge(obj);
        //  String JudgeResult= _dao.Judge(NewCode);
        return JudgeResult;
    }
    /*
     将测评函数和用户代码合并起来
     */
    public static String  MergeCode(String UserCode,String JudgeCode){
        StringBuilder builder=new StringBuilder();
        if (UserCode.charAt(UserCode.length()-1)=='}') {
            builder.append(UserCode.substring(0, UserCode.length() - 1));
            builder.append(JudgeCode);
            builder.append("}");
        }else{
            builder.append(UserCode);
            builder.append("\n");
            builder.append(JudgeCode);
        }
        return builder.toString();
    }
    public static List<Integer> test(){
        ArrayList<Integer> list=new ArrayList<>();
        for (int i=0;i<101;i++){
            if (i%2==0)
                list.add(i);
        }
        return list;
    }


}
